from matplotlib import pyplot
from numpy import arange
import bisect

class Plotter(object):


    def scatterplot(self,x,y):
        pyplot.plot(x,y,'b.')
        pyplot.xlim(min(x)-1,max(x)+1)
        pyplot.ylim(min(y)-1,max(y)+1)
        pyplot.show()

    def barplot(self,labels,data):
        pos=arange(len(data))
        pyplot.xticks(pos+1.4,labels)
        pyplot.bar(pos,data)
        pyplot.show()

    def histplot(self,data,bins=None,nbins=5):
        if not bins: 
            minx,maxx=min(data),max(data)
            space=(maxx-minx)/float(nbins)
            bins=arange(minx,maxx,space)
            binned=[bisect.bisect(bins,x) for x in data]
            l=['%.1f'%x for x in list(bins)+[maxx]] if space<1 else [str(int(x)) for x in list(bins)+[maxx]]
            displab=[x+'-'+y for x,y in zip(l[:-1],l[1:])]
            self.barplot(displab,[binned.count(x) for x in range(len(bins)+1)])

    def barchart(self,x,y,numbins=5):
        datarange=max(x)-min(x)
        bin_width=datarange/numbins
        pos=min(x)
        bins=[0 for i in range(numbins+1)]
        for i in range(numbins):
            bins[i]=pos
            pos+=bin_width
            bins[numbins]=max(x)
            binsum=[0 for i in range(numbins)]
            bincount=[0 for i in range(numbins)]
            binaverage=[0 for i in range(numbins)]
            for i in range(numbins):
                for j in range(len(x)):
                    if x[j]>=bins[i] and x[j]<bins[i+1]:
                        bincount[i]+=1
                        binsum[i]+=y[j]
                        for i in range(numbins):
                            binaverage[i]=binsum[i]/bincount[i]
                            self.barplot(range(numbins),binaverage)

    def piechart(self,labels,data):
        fig=pyplot.figure(figsize=(7,7))
        pyplot.pie(data,labels=labels,autopct='%1.2f%%')
        pyplot.show()